package com.mrd.gtimp.business.repository;

import com.mrd.gtimp.business.entity.Declaration;
import com.mrd.spring.repository.BaseRepository;

import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * application name: gtimp
 * <p>
 * copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author luob
 */
public interface DeclarationDao extends BaseRepository<Declaration, String> {

    @Transactional(value = "transactionManager")
    List<Declaration> findByIdIn(List<String> ids);

    /**
     * 根据指定id和状态查询订单
     *
     * @param id    id
     * @param state 指定状态
     * @return 结果
     */
    Declaration findByIdAndState(String id, String state);

    /**
     * 根据指定id和状态查询订单
     *
     * @param ids   ids
     * @param state 指定状态
     * @return 结果
     */
    Declaration findByIdInAndState(List<String> ids, String state);

    /**
     * 查询报关单状态和支付状态
     *
     * @param declarationId 报关单ID
     */
    @Query("select t.state, t.payment_type from com.mrd.gtimp.business.entity.Declaration t where t.id = ?")
    List<Object[]> findByStateAndPaymentType(String declarationId);

    /**
     * 用于测试
     * 查找3.0中没有存2.0实际托运人的报关单
     */
    List<Declaration> findByCustomerIsNotNullAndShipperIsNull();

    /**
     * 通过业务编码查询ID
     */
    @Query("select t.id from com.mrd.gtimp.business.entity.Declaration t where t.businessId = ?")
    Object findIdByBusinessId(String businessId);

    /**
     * 通过业务编号查询报关单
     */
    @Query("from com.mrd.gtimp.business.entity.Declaration t where t.state!='DS03' and t.state!='DS030' and t.businessId = ?")
    Declaration findEntityByBusinessId(String businessId);

    @Query("from com.mrd.gtimp.business.entity.Declaration t where t.state!='DS03' and t.state!='DS030' and t.container_no like ?")
    List<Declaration> findByContainerNo(String containerNo);
}
